﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace WP7.Extensions.Extensions
{
    public static class ObjectExtensions
    {
        /// <summary>
        /// Checks if object is null
        /// </summary>
        /// <param name="self"></param>
        /// <returns>Returns true if object is null</returns>
        public static bool IsNull(this object self)
        {
            return self == null;
        }

        /// <summary>
        /// Checks if object is not null
        /// </summary>
        /// <param name="self"></param>
        /// <returns>Returns true if object is not null</returns>
        public static bool IsNotNull(this object self)
        {
            return self != null;
        }

        /// <summary>
        /// Checks if objects are equal
        /// </summary>
        /// <param name="self">Your object</param>
        /// <param name="obj">Object to check equals on</param>
        /// <returns>Returns true if objects are not null and are equal</returns>
        public static bool NullTolerantEquals(this object self, object obj)
        {
            if (self.IsNull() && obj.IsNotNull())
                return false;

            if (self.IsNotNull() && obj.IsNull())
                return false;

            if (self.IsNull() && obj.IsNull())
                return true;

            return self.Equals(obj);
        }

        /// <summary>
        /// Checks if object exists in collection
        /// </summary>
        /// <param name="self">Your object</param>
        /// <param name="enumerable">Collection to check</param>
        /// <returns>True if object exists in collection</returns>
        public static bool In(this object self, IEnumerable enumerable)
        {
            return enumerable.IsNotNull() && enumerable.Cast<object>().Contains(self);
        }
    }
}
